﻿Public Class FormMovMaterialesListado

    Private Sub FormMovMaterialesListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Dim classController As New Class_RC_Controller.rc_Movimientos()
    Dim notas As New Class_RC_Controller.rc_Notas
    Dim ventas As New Class_RC_Controller.rc_Facturas

    Private m_frmEntradaMaterial As FrmEntradaMateriales
    Public Property frmEntradaMaterial() As FrmEntradaMateriales
        Get
            Return m_frmEntradaMaterial
        End Get
        Set(ByVal value As FrmEntradaMateriales)
            m_frmEntradaMaterial = value
        End Set
    End Property

    Private m_frmSalidaMaterial As FrmSalidaMateriales
    Public Property frmSalidaMaterial() As FrmSalidaMateriales
        Get
            Return m_frmSalidaMaterial
        End Get
        Set(ByVal value As FrmSalidaMateriales)
            m_frmSalidaMaterial = value
        End Set
    End Property

    Private Sub CargarMovimiento()

        Try
            Dim nroPedido As Integer = 0

            If txtNroPedido.Text <> "" Then
                If IsNumeric(txtNroPedido.Text) Then
                    nroPedido = txtNroPedido.Text
                Else
                    txtNroPedido.Text = ""
                End If
            End If

            Dim tipoMov As String = ""
            If cbxTipoMov.Checked Then
                tipoMov = cboTipoMov.Text
            End If

            Dim dt As New DataTable
            dt = classController.get_MovimientosMaterial_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, tipoMov, txtProveedor.Text,
                                                     txtCliente.Text, nroPedido, txtNroFactura.Text, txtRUCPro.Text, txtRUCCli.Text)
            dgMovProductos.DataSource = dt
            dgMovProductos.Select()

            If Not classController.MessageError Is Nothing Then
                MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub CargarCombos()

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgMovProductos.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                Dim codNota As String
                Dim codCompra As String

                cod = dgMovProductos.SelectedRows(0).Cells(0).Value
                codNota = dgMovProductos.SelectedRows(0).Cells(1).Value.ToString()
                codCompra = dgMovProductos.SelectedRows(0).Cells(2).Value.ToString()

                If codNota <> "" Then
                    If frmSalidaMaterial Is Nothing Then

                        frmSalidaMaterial = New FrmSalidaMateriales
                        frmSalidaMaterial.codMovimiento = cod

                        'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        frmSalidaMaterial.ShowDialog()
                        CargarMovimiento()
                        frmSalidaMaterial.Dispose()
                        frmSalidaMaterial = Nothing
                        'End If
                    Else
                        frmSalidaMaterial.codMovimiento = cod
                        frmSalidaMaterial.ShowDialog()
                    End If
                End If

                If codCompra <> "" Then
                    If frmEntradaMaterial Is Nothing Then

                        frmEntradaMaterial = New FrmEntradaMateriales
                        frmEntradaMaterial.codMovimiento = cod

                        'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        frmEntradaMaterial.ShowDialog()
                        CargarMovimiento()
                        frmEntradaMaterial.Dispose()
                        frmEntradaMaterial = Nothing
                        'End If
                    Else
                        frmEntradaMaterial.codMovimiento = cod
                        frmEntradaMaterial.ShowDialog()
                    End If

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        CargarMovimiento()
    End Sub

    Private Sub txtCliente_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProveedor.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private Sub dgMovProductos_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgMovProductos.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgMovProductos_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgMovProductos.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub txtRuc_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRUCPro.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private Sub txtNro_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNroPedido.KeyDown, txtNroFactura.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private Sub txtNroFactura_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNroFactura.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private Sub cbxTipoMov_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbxTipoMov.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private Sub cboTipoMov_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboTipoMov.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private m_frmRpts As FormReportes
    Public Property frmRpts() As FormReportes
        Get
            Return m_frmRpts
        End Get
        Set(ByVal value As FormReportes)
            m_frmRpts = value
        End Set
    End Property

    'Private Sub btnImprimir_Click(sender As System.Object, e As System.EventArgs)

    '    If frmRpts Is Nothing Then
    '        frmRpts = New FormReportes()
    '        'frmRpts.MdiParent = Me
    '        frmRpts.tipo_Reporte = "VENTAS"
    '        frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
    '        frmRpts.ShowDialog()
    '    Else

    '        If Not frmRpts.Visible Then
    '            frmRpts = New FormReportes()
    '            'frmRpts.MdiParent = Me
    '            frmRpts.tipo_Reporte = "VENTAS"
    '            frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
    '            frmRpts.ShowDialog()
    '        Else
    '            frmRpts.tipo_Reporte = "VENTAS"
    '            frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
    '            frmRpts.Select()
    '            frmRpts.Activate()
    '        End If

    '    End If

    'End Sub

    Private Sub fechaInicio_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fechaInicio.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private Sub fechaFin_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fechaFin.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMovimiento()

        End If
    End Sub

    Private Sub FormMovProductosListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Iniciar()
    End Sub

    Public Sub Iniciar()
        Try

            dgMovProductos.AutoGenerateColumns = False
            fechaInicio.Value = DateTime.Now.AddDays(-5)
            cboTipoMov.SelectedIndex = 0

            CargarCombos()
            CargarMovimiento()

        Catch ex As Exception

        End Try
    End Sub
    Private Function CargarMovMaterialesReporte() As DataTable

        Dim nroPedido As Integer = 0

        If txtNroPedido.Text <> "" Then
            If IsNumeric(txtNroPedido.Text) Then
                nroPedido = txtNroPedido.Text
            Else
                txtNroPedido.Text = ""
            End If
        End If
        Dim tipoMov As String = ""
        If cbxTipoMov.Checked Then
            tipoMov = cboTipoMov.Text
        End If

        Dim dt As New DataTable
        dt = classController.get_MovimientosMaterial_Imprimir(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, tipoMov, txtProveedor.Text,
                                                     txtCliente.Text, nroPedido, txtNroFactura.Text, txtRUCPro.Text, txtRUCCli.Text)
        If Not dt Is Nothing Then
            Return dt
        Else
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return Nothing
        End If

    End Function
    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click
        If frmRpts Is Nothing Then
            frmRpts = New FormReportes()
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "MOVMATERIALES"
            frmRpts.datos_Reporte = CargarMovMaterialesReporte()
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New FormReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "MOVMATERIALES"
                frmRpts.datos_Reporte = CargarMovMaterialesReporte()
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "MOVMATERIALES"
                frmRpts.datos_Reporte = CargarMovMaterialesReporte()
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If
    End Sub

    Private Sub btnMateriales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMateriales.Click
        Dim dt As New DataTable
        dt = classController.get_Materiales_Deposito(oiml.dbConn)

        If frmRpts Is Nothing Then
            frmRpts = New FormReportes()
            frmRpts.tipo_Reporte = "MATERIALESDEPOSITO"
            frmRpts.datos_Reporte = dt
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New FormReportes()
                frmRpts.tipo_Reporte = "MATERIALESDEPOSITO"
                frmRpts.datos_Reporte = dt
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "MATERIALESDEPOSITO"
                frmRpts.datos_Reporte = dt
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If
    End Sub
End Class